package com.sm.appbase.base.basemvp;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.shuoyue.appbase.R;


/**
 * 自定义加载框
 *
 * @author Forty'7  2018.10.24
 * xiaowangboke@vip.qq.com
 */
public class SpotsDialog extends AlertDialog {
    private Context context;
    private TextView txtInfo;
    private ProgressBar progressbar;
    private ImageView img;
    String title;

    public SpotsDialog(Context context, String title) {
        this(context, R.style.AlertDialog);
        this.context = context;
        this.title = title;
        Log.e("spotsDialog", context.toString());

    }

    public SpotsDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
        Log.e("spotsDialog", context.toString());
    }

    public SpotsDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
        Log.e("spotsDialog", context.toString());

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
        txtInfo = (TextView) view.findViewById(R.id.textView_progress);
        if (!TextUtils.isEmpty(title)) {
            txtInfo.setText(title);
        }
        progressbar = (ProgressBar) view.findViewById(R.id.dialog_progressbar);
        img = (ImageView) view.findViewById(R.id.dialog_img);
        setContentView(view);
//        setText("Loading...");
    }

    /**
     * 设置文字
     */
    public void setText(String text) {
        if (txtInfo == null)
            return;

        txtInfo.setText(text);
        txtInfo.setVisibility(View.VISIBLE);
    }

    /**
     * 设置图片
     * id 图片资源
     */
    public void setImg(int id) {
        if (img == null)
            return;
        img.setImageResource(id);
        img.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏/显示加载圈
     * state 状态 true显示 false 隐藏
     */
    public void isShow(boolean state) {
        if (progressbar == null)
            return;
        if (state) {
            progressbar.setVisibility(View.VISIBLE);
        } else {
            progressbar.setVisibility(View.GONE);
        }
    }

    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
